/**
 * @param {number[][]} grid
 * @return {number}
 */

var minPathSum = function (grid) {
    let row = grid.length
    let col = grid[0].length
    // 初始化二维数组
    let dp = new Array(row).fill(0)
    for (let i = 0; i < row; i++) {
        dp[i] = new Array(col).fill(0)
    }
    for (let i = 0; i < row; i++) {
        for (let j = 0; j < col; j++) {
            // 左上角元素
            if (i === 0 && j === 0) {
                dp[i][j] = grid[0][0]
            }
            // 第一行元素
            if (i === 0 && j !== 0) {
                dp[i][j] = grid[i][j] + dp[i][j - 1]
            }
            // 第一列元素
            if (j === 0 && i !== 0) {
                dp[i][j] = grid[i][j] + dp[i - 1][j]
            }
            if (i !== 0 && j !== 0) {
                dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1])
            }
        }
    }
    return dp[row - 1][col - 1]
};
console.log(minPathSum([[1, 3, 1], [1, 5, 1], [4, 2, 1]]))